import { _decorator, Component, Enum, Node, Quat, tween, Vec3 } from 'cc';
import { clientEvent } from '../framework/clientEvent';
import { constant } from '../framework/constant';
import { effectManager } from '../framework/effectManager';
import { audioManager } from '../framework/audioManager';
import { resourceUtil } from '../framework/resourceUtil';
import { poolManager } from '../framework/poolManager';
const { ccclass, property } = _decorator;
//奖励类型
const REWARD_TYPE = Enum({
    GOLD: 1,//金币
    HEART: 2,//爱心
})
@ccclass('reward')
export class reward extends Component {

    @property({
        type: REWARD_TYPE,
        displayOrder: 1
    })
    public rewardType: any = REWARD_TYPE.GOLD;
    public isDropOver: boolean = false;//是否已经落在地上

    private _trialScale = new Vec3(3, 3, 3);//拖尾缩放
    private _trialPos: Vec3 = new Vec3(0, -0.1, 0);//拖尾位置
    private _tweenBounce: any = null!;//tween
    private _curQuat: Quat = new Quat();//旋转
    private _isAutoRotate: boolean = false;//是否自动旋转
    private _isInhaling: boolean = false;//是否正在吸入中
    private _oriScale: Vec3 = null!;//原始缩放大小
    private _ndParent: Node = null!;//父亲节点
    private _ndTrial: Node = null!;//拖尾节点
    private _endTargetPos: Vec3 = new Vec3();//最终目标位置
    private _midTargetPos: Vec3 = new Vec3();//中间位置
    private _stepTargetPos: Vec3 = new Vec3();//下一次的位置
    private _upSpeedY: number = 0.2;//上升最大速度
    private _downSpeedY: number = 0.2;//下降最大速度
    private _curSpeedY: number = 0;//当前速度
    private _oriWorPos: Vec3 = new Vec3();//初始奖品世界坐标
    private _isArriveMinPos: boolean = false;//已经到达中间位置
    private _rewardWorPos: Vec3 = new Vec3();//当前奖品世界坐标
    private _playerWorPos: Vec3 = new Vec3();//目标(主角)位置
    private _nextWorPos: Vec3 = new Vec3();//下一个位置
    private _offsetPos: Vec3 = new Vec3();//奖品和玩家之间的向量差
    private _targetWorPos: Vec3 = new Vec3();//
    private _totalFlyTime: number = 0;//奖品总飞行时间
    private _curFlyTime: number = 0;//奖品当前飞行时间
    private _raiseTimes: number = 1;//
    private _bouncePos: Vec3 = new Vec3(0, 0.618, 0);//回收缓动高度
    private _bounceScale: Vec3 = new Vec3(0.2, 0.2, 0.2);//回收缓动缩放

    protected _closeTween() {
        if (this._tweenBounce) {
            this._tweenBounce.stop();
            this._tweenBounce = null!;
        }
    }

    /**
     * 玩家吸入奖品
     *
     * @protected
     * @memberof Reward
     */
    protected _inhaleReward() {
        //先弹跳
        this._closeTween();
        this._tweenBounce = tween(this.node)
            .by(0.3, { position: this._bouncePos, scale: this._bounceScale }, { easing: "bounceInOut" })
            .call(() => {
                //播放粒子特效，不要勾选粒子的prewarm属性，免得出现概率性没有播放拖尾
                this._ndTrial.active = true;
                effectManager.instance.playTrail(this._ndTrial);

                //再吸入
                this._isInhaling = true;

            })
            .start()
    }

    onEnable() {
        clientEvent.on(constant.EVENT_TYPE.INHALE_REWARD, this._inhaleReward, this);
    }

    onDisable() {
        clientEvent.off(constant.EVENT_TYPE.INHALE_REWARD, this._inhaleReward, this);
    }


    show() {
        this.node.active = true;
        let x = Math.random() * 6 - 3;//-3~3
        let y = 4;//最高的高度4~4.5;
        let z = Math.random() * 6 - 3;//-3~3
        this._endTargetPos = this._endTargetPos.set(this._oriWorPos).add3f(x, 0, z);
        this._midTargetPos = this._midTargetPos.set(this._oriWorPos).add3f(x / 2, y, z / 2);

        // console.log("终点位置", this._endTargetPos, "中间位置", this._midTargetPos);

        audioManager.instance.playSound(constant.SOUND.GOLD_DROP);
    }

    init(time: number, ndParent: Node) {
        this._ndParent = ndParent;

        if (this._oriScale) {
            this.node.setScale(this._oriScale);
        } else {
            this._oriScale = this.node.getScale();
        }

        this._isAutoRotate = false;
        this._isInhaling = false;
        this._curSpeedY = this._upSpeedY;
        this._oriWorPos.set(this.node.getWorldPosition().x, 1.65, this.node.getWorldPosition().z);
        this._isArriveMinPos = false;
        this.isDropOver = false;
        this._totalFlyTime = 0;
        this._curFlyTime = 0;
        this._raiseTimes = 1;

        //依次弹出奖品
        this.scheduleOnce(() => {
            this.show();
        }, time)

        if (!this._ndTrial) {
            resourceUtil.loadEffectRes("trail/coinTrail").then((pf: any) => {
                this._ndTrial = poolManager.instance.getNode(pf, this.node);
                this._ndTrial.active = false;
                this._ndTrial.setScale(this._trialScale);
                this._ndTrial.setPosition(this._trialPos);
            });
        } else {
            this._ndTrial.active = false;
        }
    }
}


